/**
 * Project of Vehicle Routing Problem with Time Windows implements 
 * some of well-known algorithms for solving VRPTW and presents new 
 * one.
 *
 * @author Michal Drobny  
 */

package logic.timewindow;

/**
 * Represents a time window. The time window can be defined in destination just
 * like another condition which must be kept for solving VRPTW. The generic T
 * represent the type of bounds in time window - usually Long.
 * 
 * @author Michal Drobny
 * @date 3.10.2014
 */
public class TimeWindow<T extends Comparable<T>> implements
		Comparable<TimeWindow<T>> {

	private final T start;
	private final T end;

	/**
	 * Constructor.
	 * 
	 * @param start
	 *            The start time
	 * @param end
	 *            The end time
	 */
	public TimeWindow(T start, T end) {
		super();
		this.start = start;
		this.end = end;
	}

	/**
	 * @return the start
	 */
	public T getStart() {
		return start;
	}

	/**
	 * @return the end
	 */
	public T getEnd() {
		return end;
	}

	/**
	 * Checks, if this time window is included in time window given as
	 * parameter.
	 * 
	 * @param timewindow
	 *            Time window
	 * @return Whether this time window is included in time window specified as
	 *         a parameter
	 */
	public boolean isIncluded(TimeWindow<T> timewindow) {
		if (timewindow == null)
			return true;

		return (getStart().compareTo(timewindow.getStart()) > -1)
				&& (getEnd().compareTo(timewindow.getEnd()) < 1);
	}

	/**
	 * Checks, if this time window included given value.
	 * 
	 * @param value
	 *            The value
	 * @return Whether this time window included given value
	 */
	public boolean include(T value) {
		if (value == null)
			return true;

		return (getStart().compareTo(value) < 1)
				&& (getEnd().compareTo(value) > -1);
	}

	@Override
	public int compareTo(TimeWindow<T> timewindow) {
		if (timewindow == null)
			return 1;
		if (getStart().compareTo(timewindow.getEnd()) > -1)
			return 1;
		if (getEnd().compareTo(timewindow.getStart()) < 1)
			return -1;
		return 0;
	}
}